﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MPMusicVids.Data;
using System.IO;
using MediaPortal.Configuration;
using MPMusicVids.Log;
using SQLite.NET;

namespace MPMusicVids
{
    public partial class Settings : UserControl
    {
        DataManager dm = MusicVidsMain.dm;

        public Settings()
        {
            InitializeComponent();
        }        

        private void radioButton4_CheckedChanged(object sender, EventArgs e)
        {
            if (txtAdvancedExpression.Enabled)
            {
                txtAdvancedExpression.Enabled = false;
                txtAdvancedExpression.Text = "";
            }
            else
            {
                txtAdvancedExpression.Enabled = true;
                lblExpExample.Text = "";
            }
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            lblExpExample.Text = "Metallica - Enter Sandman.avi";
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            lblExpExample.Text = "Music Videos\\Metallica\\Enter Sandman.avi";
        }

        private void radioButton3_CheckedChanged(object sender, EventArgs e)
        {
            lblExpExample.Text = "Music Videos\\Metallica\\Metallica\\Enter Sandman.avi";
        }

        private void txtAdvancedExpression_TextChanged(object sender, EventArgs e)
        {
            lblExpExample.Text = txtAdvancedExpression.Text;
            lblExpExample.Text = lblExpExample.Text.Replace("%artist%", "Metallica")
                .Replace("%album%", "Metallica").Replace("%title%", "Enter Sandman")
                .Replace("%misc%", "Some Text").Replace("%ext%", "avi");
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            ExprHelp helper = new ExprHelp();
            helper.Show();
        }

        private void btnAddWatch_Click(object sender, EventArgs e)
        {
            string exp = "";
            if (rdoAdv.Checked)
                exp = txtAdvancedExpression.Text;
            if (rdoExp1.Checked)
                exp = "%artist% - %title%.%ext%";
            if (rdoExp2.Checked)
                exp = "%artist%\\%title%.%ext%";
            if (rdoExp3.Checked)
                exp = "%artist%\\%album%\\%title%.%ext%";
            //Save to db and update gridview
            if (txtWatchFolder.Text != "")
            {
                dm.addWatch(txtWatchFolder.Text, exp);
                LoadSettings();
            }
            else
                MessageBox.Show("The watch-folder field cannot be blank");
        }

        public void LoadSettings()
        {
            //log.Append("Loading settings from Database");
            //Populate DGV to show watch folders
            SQLiteResultSet watchFolders = dm.getWatch();
            SQLiteResultSet settingsRS = dm.getSettings();
            dgvWatchFolders.Rows.Clear();
            foreach (SQLiteResultSet.Row row in watchFolders.Rows)
            {
                dgvWatchFolders.Rows.Add(row.fields[1], row.fields[2], row.fields[0]);
            }
            foreach (SQLiteResultSet.Row row in settingsRS.Rows)
            {
                txtFFMpeg.Text = row.fields[0];
                txtThumbTime.Text =  row.fields[1];
                txtPluginName.Text = row.fields[2];
                txtExtensions.Text = row.fields[3];
                txtDT.Text = row.fields[5];
            }
        }

        private void txtPluginName_TextChanged(object sender, EventArgs e)
        {
            dm.Execute("UPDATE Settings SET pluginName='" + txtPluginName.Text + "'");
        }

        private void txtFFMpeg_TextChanged(object sender, EventArgs e)
        {
            dm.Execute("UPDATE Settings SET ffmpeg='" + txtFFMpeg.Text + "'");
        }

        private void txtThumbTime_TextChanged(object sender, EventArgs e)
        {
            dm.Execute("UPDATE Settings SET thumbTime='" + txtThumbTime.Text + "'");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            getFFMPEG.InitialDirectory = @"C:\";
            getFFMPEG.Title = "Select ffmpeg.exe";
            getFFMPEG.Filter = "ffmpeg.exe|ffmpeg.exe";
            getFFMPEG.FilterIndex = 1;

            if (getFFMPEG.ShowDialog() != DialogResult.Cancel)
            {
                txtFFMpeg.Text = getFFMPEG.FileName;
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (getWatchFolder.ShowDialog() != DialogResult.Cancel)
            {
                txtWatchFolder.Text = getWatchFolder.SelectedPath;
            }
        }

        private void button8_Click(object sender, EventArgs e)
        {
            mainConfig mc = (mainConfig)this.Parent.Parent.Parent;
            mc.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Rescan rs = new Rescan();
            if (rs.ShowDialog() == DialogResult.OK)
            {
                TabControl tc1 = (TabControl)this.Parent.Parent;
                tc1.SelectedIndex = 0;
            }
        }

        private void dgvWatchFolders_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            MessageBoxButtons buttons = MessageBoxButtons.YesNo;
            if (MessageBox.Show("Are you sure you want to delete this watch folder?",
                "Confirm watch folder deletion", buttons, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                int watchID = int.Parse(dgvWatchFolders.Rows[e.RowIndex].Cells[2].Value.ToString());
                dm.Execute("DELETE FROM Folders WHERE id = " + watchID);
            }
            LoadSettings();
        }

        private void txtExtensions_TextChanged(object sender, EventArgs e)
        {
            dm.Execute("UPDATE Settings SET extensions='" + txtExtensions.Text + "'");
        }

        private void button3_Click(object sender, EventArgs e)
        {
            getDT.InitialDirectory = @"C:\Program Files";
            getDT.Title = "Find Daemon Tools Location";
            getDT.Filter = "All Files|*.*";
            getDT.FilterIndex = 1;
            if (getDT.ShowDialog() != DialogResult.Cancel)
            {
                txtDT.Text = getDT.FileName;
            }
        }

        private void textDT_TextChanged(object sender, EventArgs e)
        {
            dm.Execute("UPDATE Settings SET DT='" + txtDT.Text + "'");
        }

        private void button5_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start(Config.GetFile(Config.Dir.Log, "MusicVids.log"));
        }
    }
}
